<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="莫吉托" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 组织架构 -->
        <el-tree :data="list" :default-expand-all="true" :props="{children: 'children',label: 'name'}">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="莫吉托" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length ===0"
                          @click.native="hDelete(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        :title="title"
      >
        <AddOrEdit
          v-if="showDialog"
          ref="dialong"
          :cur-id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="huSuccess"
          @close="holose"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
// 导入ajax
import { getDepartments, delDepartment } from '@/api/departments'
import { tranList } from '@/utils'
import AddOrEdit from './deptDialong.vue'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      title: '新增',
      // 弹层开关
      showDialog: false,
      // 列表数据
      list: [],
      // 获取id
      curId: '',
      // 控制新增还是编辑弹层的复用
      isEdit: false,
      // 为了控制 添加不能重复元素 定义的数据
      originList: []
    }
  },
  created() {
    // 触发请求
    this.loadDepartments()
  },
  methods: {
    // 获取组织架构
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 删除第一条标题数据并将它添加进data
        res.data.depts.shift()
        this.list = tranList(res.data.depts)
        console.log(res, '列表架构')
        // 筛选出筛选表单不能重复的代码
        this.originList = res.data.depts.map(({ code, id, pid, name }) => { return { code, id, pid, name } })
        console.log(this.originList)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增弹层
    hAdd(id) {
      this.isEdit = false // false代表新增
      this.title = '新增'
      this.showDialog = true
      this.curId = id
    },
    // 编辑弹层
    hEdit(id) {
      this.isEdit = true // true代表编辑
      this.title = '修改'
      this.showDialog = true
      this.curId = id
      // 引入子组件中的loadAdd()重新获取部门详情
      // 重新填充到表单
      // 方法二: 解决编辑表单数据不更新的问题
      // settimeout:保存dom异步更新完成后再执行
      // setTimeout(() => {
      //   this.$refs.dialong.loadAdd()
      // }, 0)
    },
    // 关闭弹层
    holose() {
      this.showDialog = false
      this.$refs.dialong.resetrom()
    },
    huSuccess() {
    // 关闭弹层
      this.showDialog = false
      // 重新渲染数据
      this.loadDepartments()
    },
    // 删除函数
    hDelete(id) {
      this.$confirm('是否删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        this.$message.success('删除成功')
        // 刷新页面
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>
<style scoped>

</style>
